/*
 * Copyright (c) $today.year-Now http://www.zxpnet.com All rights reserved.
 */

package com.zxp.springboot.hello.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxp.springboot.hello.model.User;
import com.zxp.springboot.hello.service.UserServiceI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: shollin
 * @date: 2021/6/27/027 17:28
 */
@RestController
@Slf4j
public class UserController {


    @Autowired
    UserServiceI userService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/")
    public User get(Long id){

        Object o = redisTemplate.opsForValue().get(id+"");
        if(ObjectUtils.isNotEmpty(o)){
            return (User) o;
        }

        User user= userService.getById(id);
        redisTemplate.opsForValue().set(id+"", user);

        return user;
    }

    @GetMapping("/page")
    public Page<User>  page(@RequestParam(name = "page", defaultValue = "1") Integer page,
                     @RequestParam(name = "rows", defaultValue = "10") Integer rows){
        Page<User> pn = new Page<>(page,rows);
        return userService.page(pn);
    }

    @GetMapping("/save")
    public User save(User user){
        boolean save = userService.save(user);
        return user;
    }


}
